package cn.maigc.web.sys_role.controller;

import cn.maigc.utils.ResultVo;
import cn.maigc.web.sys_role.entity.RoleParam;
import cn.maigc.web.sys_role.entity.SelectItem;
import cn.maigc.web.sys_role.entity.SysRole;
import cn.maigc.web.sys_role.service.SysRoleService;
import cn.maigc.web.sys_role_menu.entity.SaveMenuParam;
import cn.maigc.web.sys_role_menu.service.SysRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/role")
@Tag(name = "角色管理接口",description = "角色管理")
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @GetMapping("/getList")
    @Operation(summary = "角色分页查询")
    public ResultVo getList(RoleParam param){

        //构建分页对象
        IPage<SysRole> page = new Page<>();
        //当前页、大小
        page.setCurrent(param.getCurPage());
        page.setSize(param.getPageSize());
        //构造查询条件
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param.getRoleName())){
            wrapper.lambda().like(SysRole::getRoleName,param.getRoleName());
        }
//        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotEmpty(param.getRoleName()),SysRole::getRoleName,param.getRoleName());
        IPage<SysRole> pages = sysRoleService.page(page, wrapper);
        if(pages != null){
            return ResultVo.success("查询成功！",pages);
        }
        return ResultVo.error("查询失败！！");
    }
    //增加
    @Operation(summary = "增加角色")
    @PostMapping
    public ResultVo add(@RequestBody SysRole sysRole){
        sysRole.setCreateTime(new Date());
        boolean temp = sysRoleService.save(sysRole);
        if(temp){
            return ResultVo.success("新增成功！");
        }
        return ResultVo.error("新增失败！");
    }
    //删除
    @Operation(summary = "删除角色")
    @DeleteMapping("{roleId}")
    public ResultVo delete(@PathVariable("roleId")Long roleId){
        boolean temp = sysRoleService.removeById(roleId);
        if(temp){
            return ResultVo.success("删除成功！");
        }
        return ResultVo.error("删除失败！");
    }

    //修改
    @Operation(summary = "修改角色")
    @PutMapping
    public ResultVo edit(@RequestBody SysRole sysRole){
        sysRole.setUpdateTime(new Date());
        boolean temp = sysRoleService.updateById(sysRole);
        if(temp){
            return ResultVo.success("编辑成功！");
        }
        return ResultVo.error("编辑失败！");
    }

    //获取角色下拉数据
    @Operation(summary = "获取角色下拉数据")
    @GetMapping("/selectList")
    public ResultVo selectList(){
        List<SysRole> list = sysRoleService.list();
        //封装返回值
        List<SelectItem> selectItems = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item -> {
                    SelectItem vo = new SelectItem();
                    vo.setCheck(false);
                    vo.setValue(item.getRoleId());
                    vo.setLabel(item.getRoleName());
                    selectItems.add(vo);
                });
        return ResultVo.success("查询成功！",selectItems);
    }

    //保存角色菜单
    @Operation(summary = "保存角色菜单")
    @PostMapping("/saveRoleMenu")
    public ResultVo saveRoleMenu(@RequestBody SaveMenuParam param){
        sysRoleMenuService.saveRoleMenu(param);
        return  ResultVo.success("分配成功");
    }
}
